import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig, header} from '../data/config'

const base = url.base,
  bank = url.bank,
  minshengBank = url.minshengBank,
  getOnePCFail = request.getOnePCFail,
  capital = request.capital;

let table,
  bankUrl,
  storeData;

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + getOnePCFail,
  queryParams: function (params) {
    return $.extend({
      currentPage: params.pageNumber,
      pageSize: params.pageSize
    }, $("#searchForm").serializeObject())
  },
  columns: [
    {
      field: 'id',
      title: '业务流水号',
      valign: 'middle'
    }, {
      field: 'propertyName',
      title: '楼盘名称',
      valign: 'middle'
    }, {
      field: 'termId',
      title: '终端号',
      valign: 'middle'
    }, {
      field: 'tradeAmount',
      title: '担保金金额',
      valign: 'middle',
      formatter: function (index, row) {
        return (index / 100).toFixed(2)
      }
    }, {
      field: 'payAmount',
      title: '已支付金额',
      valign: 'middle',
      formatter: function (index, row) {
        return (index / 100).toFixed(2)
      }
    }, {
      field: 'name',
      title: '姓名',
      valign: 'middle'
    }, {
      field: 'bankNum',
      title: '返还担保金借记卡卡号',
      valign: 'middle'
    }, {
      field: 'houseBuyName',
      title: '购房人姓名',
      valign: 'middle'
    }, {
      field: 'tradeDate',
      title: '交易时间',
      valign: 'middle',
      formatter: function (index, row) {
        return moment(index).format('YYYY-MM-DD HH:mm:ss')
      }
    }, {
      field: 'action',
      valign: 'middle',
      align: 'center',
      title: '操作',
      formatter: function (index, row) {
        return `<div class="btn-group">
                      <button type="button" data-id="${ row.id }" class="btn btn-default capital">验资</button>                              
                </div>`
      }
    }]
}))

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
  storeData = data;
  console.log('in data')
  console.log(storeData, storeData.channelCode)
  switch (storeData.channelCode) {
    case 'minsheng':
      bankUrl = minshengBank
      break;
    default:
      bankUrl = bank
  }
})

// 初始化日期选择器
$('#exchangeBeginTime').datetimepicker({
  format: "yyyy-mm-dd",
  startView: 3,
  minView: 2,
  autoclose: true
});
$('#exchangeEndTime').datetimepicker({
  format: "yyyy-mm-dd",
  startView: 3,
  minView: 2,
  autoclose: true
});

// 搜索
$('#search-btn').on('click', (e) => {
  e.preventDefault();

  table.bootstrapTable('refresh', {
    query: $.extend({
      currentPage: 1,
    }, $('#searchForm').serializeObject())
  })
})

// 验资
$(document).on('click', '#table .capital', (e) => {
  e.preventDefault()

  console.log('验资')

  let id = $(e.target).attr('data-id'),
    data = {identOrderId: id, isPass: 'no'}

  let loading = layer.msg('验资中...', {
    icon: 16,
    time: 0,
    shade: .3
  })

  $.ajax({
    headers: header,
    url: bankUrl + capital,
    data: data
  })
    .done(function (res) {
      console.log(res)

      if (res.code === 0) {

        bootbox.alert(`<h3>${ res.message }</h3>`)

        table.bootstrapTable('refresh')

      } else {
        bootbox.alert(`<h3>${ res.message }</h3>`)
      }

    })
    .fail(function (res) {
      console.log(res)

      layer.msg('验资失败', {
        icon: 5
      })

    })
    .always(function (res) {

      layer.close(loading)

    })
})
